package org.ddd.orderdemo.api.controller;

import jakarta.annotation.Resource;
import org.ddd.orderdemo.application.convertor.OrderConvertor;
import org.ddd.orderdemo.application.pojo.cmd.OrderCreateCmd;
import org.ddd.orderdemo.application.pojo.response.OrderResponse;
import org.ddd.orderdemo.application.service.OrderService;
import org.ddd.orderdemo.domain.model.order.Order;
import org.springframework.http.ResponseEntity;
import org.springframework.web.bind.annotation.*;

import java.util.List;

@RestController
@RequestMapping("/order")
public class OrderController {

    @Resource
    private OrderService orderService;


    @GetMapping
    public List<Order> getAllOrders() {
        return orderService.getAllOrders();
    }


    @GetMapping("/{id}")
    public ResponseEntity<Order> getOrderById(@PathVariable Long id) {
//        Optional<Order> optionalProduct = orderService.findById(id);

        return orderService.findById(id)
                .map(ResponseEntity::ok)
                .orElse(ResponseEntity.notFound().build());
    }

    @PostMapping(name = "/create")
    public OrderResponse create(OrderCreateCmd cmd) {
        return OrderConvertor.convertToResponse(orderService.createOrder(cmd));
    }


    @PutMapping("/{id}/status")
    public ResponseEntity<Void> updateStock(
            @PathVariable Long id,
            @RequestParam Integer status) {
        orderService.updateStatus(id, status);
        return ResponseEntity.ok().build();
    }

}
